package com.hippy.erya.modular.util.excel;

import com.alibaba.excel.EasyExcel;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;



public class EasyExcelUtils {

    public static void write(List<?> list, Class<?> pojoClass, String fileNameStr, String sheetName, HttpServletResponse response)
            throws Exception {
        String fileName = new String(fileNameStr.getBytes(), StandardCharsets.ISO_8859_1);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        ServletOutputStream outputStream = response.getOutputStream();
        EasyExcel.write(outputStream, pojoClass)
                .registerWriteHandler(new ExcelWidthStyleStrategy())
                .sheet(sheetName)
                .doWrite(list);
    }
}

